﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Commands.User.Commands.Order;
using DotNet.Common.Command;
using ZLCMS.Commands.User.Util;
using NHibernate;
using ZLCMS.Commands.Data;
using System.IO;
using Newtonsoft.Json;
using ZLCMS.Commands.User.Commands.Reconciliations;
using ZLCMS.Data.Model;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Reconciliations
{
    public partial class BillDailyProductDetail : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected string uAcc{get;set;}
        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ分页控件
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);

            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                IList<SysDepartmentInfo> deptList = session.CreateQuery(" from SysDepartmentInfo where DeptCode<>'0001' ")
                    .List<SysDepartmentInfo>();
                deptList.Insert(0, new SysDepartmentInfo() { DeptCode = "-1", DeptName = "--请选择--" });
                DeptCode.DataSource = deptList;
                DeptCode.DataTextField = "DeptName";
                DeptCode.DataValueField = "DeptCode";
                DeptCode.DataBind();
            }

            LoadBillDailyProductDetailParameter param = new LoadBillDailyProductDetailParameter();

            param.StartDate = string.IsNullOrEmpty(Request["start"]) ? "" : Request["start"];
            start.Text = string.Format("{0:yyyy-MM-dd}", Request["start"]);

            param.EndDate = string.IsNullOrEmpty(Request["end"]) ? "" : Request["end"];
            end.Text = string.Format("{0:yyyy-MM-dd}", Request["end"]);


            param.ProductSkuNo = string.IsNullOrEmpty(Request["sku"]) ? "" : Request["sku"];
            param.TradeKind = string.IsNullOrEmpty(Request["k"]) ? "" : Request["k"];

            uAcc = UserKit.GetUserAccount();

            param.DeptCode = "";
            if (AuthUtil.CheckAuthorization("BILL_MAN", "VIEW_ALL", uAcc))
            {
                param.DeptCode = string.IsNullOrEmpty(Request["deptcode"]) ? "-1" : Request["deptcode"];
                DeptCode.SelectedValue = param.DeptCode;
                param.TradeKind = string.IsNullOrEmpty(Request["tk"]) ? "-1" : Request["tk"];
                ddl_TradeKind.SelectedValue = param.TradeKind;
            }
            else if (AuthUtil.CheckAuthorization("BILL_MAN", "VIEW_OWNER", uAcc))
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    param.DeptCode = session.CreateSQLQuery("select dept_code from sys_user_info where user_account=:acc")
                        .SetParameter("acc", uAcc).UniqueResult<string>();
                }
            }
            else
            {
                //没有权限访问
                param.DeptCode = "no_dept";
            }


            if (!string.IsNullOrEmpty(Request["exportexcel"]))
            {
                //设置分页大小
                param.PageSize = 9999;
                //设置页码
                param.PageIndex = 1;

                //ICommandResult<LoadBillDailyProductDetailResult> result = new LoadBillDailyProductDetailCommand().Execute(param);
                

                //StringWriter sw = new StringWriter();

                //sw.WriteLine("订单号\t快递单号\t快递代码\t支付金额\t收货人\t收货电话\t收货地址\t下单时间\t订单产品\t备注\t订单状态");
                //foreach (ShopBillWeekly item in result.Result.List)
                //{
                 
                //    sw.WriteLine("'" + item.OrderNo + "\t'" + item.ShippingNo + "\t" + GetShippingCom(item.ShippingType) + "\t'" + string.Format("{0:F2}", item.PaymentPrice)
                //        + "\t" + item.ReceiverName.Trim().Replace("\n", "") + "\t'" + item.ReceiverPhone.Trim().Replace("\n", "") 
                //        + "\t" + item.ReceiverAddress.Trim().Replace("\n","")
                //        + "\t" + item.OrderTime
                //        + "\t" + productsStr
                //        + "\t" + (string.IsNullOrEmpty(item.Remark.Trim()) ? "无" : item.Remark.Trim().Replace("\n", ""))
                //        + "\t["+ ParamKit.GetValue("OrderIsConfirmed", item.CommentStatus, " ")
                //        + "][" + ParamKit.GetValue("OrderPayStatus", item.PayStatus, " ")
                //        + "][" + ParamKit.GetValue("OrderIsFinish", item.FinishedStatus, " ")
                //        + "][" + ParamKit.GetValue("OrderIsCancel", item.CancelStatus, " ")
                //        + "]\t");

                //}
                //sw.Close();

                //OutputKit.OutputExcel(Response, sw);
            }
            else
            {
                //设置分页大小
                param.PageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;
                //设置页码
                param.PageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;

                ICommandResult<LoadBillDailyProductDetailResult> result = new LoadBillDailyProductDetailCommand().Execute(param);

                grid.DataSource = result.Result.List;
                grid.DataBind();
                _framePage.RecordCount = result.TotalCount;
            }

        }
   
    }
}